﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Data.Common;
using Data.Enum;
using Data.Model;
using DataAccess.AccountManage;
using DataAccess.BikeManage.logical;
using Ninject;
using WinForm.Helper;
using WinForm.Reports;

namespace WinForm.UserManage
{
    public partial class FrmCharge : Form
    {
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly IOperate _operate = Kernel.Get<IOperate>();

        public FrmCharge()
        {
            InitializeComponent();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                var zq58Logical = new Zq58Logical();
                ResultInfo<CpuAccount> cpuAccount = zq58Logical.ReadAccountInfo();
                if (cpuAccount.ResultNumber == ResultEnum.Success)
                {
                    ResultInfo<Decimal> currentMoney = zq58Logical.QueryPurse();
                    if (currentMoney.ResultNumber == ResultEnum.Success)
                    {
                        tbCpuNo.Text = cpuAccount.Obj.CpuCardNo;
                        tbUserName.Text = UtilStr.StrNameReplace(cpuAccount.Obj.DisplayName);
                        tbIdCardNo.Text = UtilStr.StrIDCardReplace(cpuAccount.Obj.IdCardNo);
                        tbCurrentTotalMoney.Text = currentMoney.Obj.ToString();
                        tabCz.Visible = true;
                        tabItem1.Visible = false;
                    }
                    else
                    {
                        errorMessage = errorMessage + currentMoney.Describe;
                    }
                }
                else
                {
                    errorMessage = cpuAccount.Describe;
                }
            }
            catch (Exception e1)
            {
                errorMessage = errorMessage + e1.Message;
            }

            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        private void btnCharge_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                var zq58Logical = new Zq58Logical();
                ResultInfo<CpuAccount> userInfo = zq58Logical.ReadAccountInfo();
                if (userInfo.ResultNumber == ResultEnum.Success)
                {
                    decimal cz = Converts.ConvertToDecimal(tbCzMoney.Text);
                    if (cz <= 0)
                    {
                        errorMessage = errorMessage + "充值金额要大于0";
                    }
                    else
                    {
                        ResultInfo<byte[]> czMoney = zq58Logical.PurseSave(Converts.ConvertToDecimal(tbCzMoney.Text));
                        if (czMoney.ResultNumber == ResultEnum.Success)
                        {
                            ResultInfo<Decimal> totalMoney = zq58Logical.QueryPurse();
                            if (totalMoney.ResultNumber == ResultEnum.Success)
                            {
                                lbToltalMoney.Text = totalMoney.Obj.ToString();

                                ResultInfo<bool> dbR =
                                    _operate.RechargeYourPurse(Converts.ConvertToDecimal(tbCurrentTotalMoney.Text),
                                        Converts.ConvertToDecimal(tbCzMoney.Text), userInfo.Obj.CpuCardId,
                                        SystemInfo.OperateAddress, SystemInfo.OperateAddressId, SystemInfo.OperatePsamId,
                                        SystemInfo.OperateId);
                                if (dbR.ResultNumber == ResultEnum.Success)
                                {
                                    if (
                                        MyMessageBox.ShowMessageBoxQuestion(
                                            string.Format("充值成功！账户初始金额为：{0}元，本次充值：{1}元，当前余额：{2}元,是否打印充值凭证？",
                                                tbCurrentTotalMoney.Text, tbCzMoney.Text,
                                                (Converts.ConvertToDecimal(tbCurrentTotalMoney.Text) +
                                                 Converts.ConvertToDecimal(tbCzMoney.Text)))) == DialogResult.Yes)
                                    {
                                        try
                                        {
                                            PrintRechargeFrm prF = new PrintRechargeFrm(new List<recharge>(){new recharge(){amount =Converts.ConvertToDecimal(tbCzMoney.Text),balance = Converts.ConvertToDecimal(tbCurrentTotalMoney.Text) +
                                                 Converts.ConvertToDecimal(tbCzMoney.Text),cardno = userInfo.Obj.CpuCardId,chargetime = DateTime.Now,initialcash = Converts.ConvertToDecimal(tbCurrentTotalMoney.Text)}}, "RechargeEvidenceRp.rdlc");
                                            prF.StartPosition = FormStartPosition.CenterScreen;
                                            prF.ShowDialog();
                                            Dispose();
                                        }
                                        catch (Exception)
                                        {
                                            Dispose();
                                        }
                                    }else
                                    {
                                        Dispose();
                                    }
                                    //tabCzFinished.Visible = true;
                                    //tabCz.Visible = false;
                                    //this.Close();
                                    //this.Dispose();
                                }
                                else
                                {
                                    //如果存入到数据库失败就删除刚刚存入的钱
                                    ResultInfo<byte[]> costMoney =
                                        zq58Logical.CostMoney(Converts.ConvertToDecimal(tbCzMoney.Text));
                                    if (costMoney.ResultNumber != ResultEnum.Success)
                                    {
                                        errorMessage = errorMessage + costMoney.Describe;
                                    }
                                    errorMessage = errorMessage + dbR.Describe;
                                }
                            }
                            else
                            {
                                //如果查询总金额失败就删除刚刚充值的钱
                                ResultInfo<byte[]> costMoney =
                                    zq58Logical.CostMoney(Converts.ConvertToDecimal(tbCzMoney.Text));
                                if (costMoney.ResultNumber != ResultEnum.Success)
                                {
                                    errorMessage = errorMessage + costMoney.Describe;
                                }
                                errorMessage = errorMessage + totalMoney.Describe;
                            }
                        }
                        else
                        {
                            errorMessage = errorMessage + czMoney.Describe;
                        }
                    }
                }
                else
                {
                    errorMessage = errorMessage + userInfo.Describe;
                }
            }
            catch (Exception e1)
            {
                errorMessage = errorMessage + e1.Message;
            }


            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }
    }
}